package com.hay.item.service;

import com.hay.item.mapper.SpecGroupMapper;
import com.hay.item.mapper.SpecParamMapper;
import com.hay.item.pojo.SpecGroup;
import com.hay.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @title: SpecificationService
 * @Author HuangYan
 * @Date: 2020/7/22 18:09
 * @Version 1.0
 */
@Service
public class SpecificationService {

    @Autowired
    SpecGroupMapper groupMapper;

    @Autowired
    SpecParamMapper paramMapper;

    /**
     * 根据商品品类查询组信息
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return groupMapper.select(specGroup);
    }

    /**
     * 根据组id查询组下面的参数列表
     * @param gid
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);
        return paramMapper.select(specParam);
    }

    /**
     * 添加新组
     * @param cid
     * @param name
     */
    public void addCategoryGroupsByCid(Long cid, String name) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        specGroup.setName(name);
        groupMapper.insert(specGroup);
    }

    /**
     * 修改组名称
     * @param id
     * @param cid
     * @param name
     * @return
     */
    public void editCategoryGroupsByCid(Long id, Long cid, String name) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setId(id);
        specGroup.setCid(cid);
        specGroup.setName(name);
        groupMapper.updateByPrimaryKey(specGroup);
    }

    /**
     * 添加组写下面的参数信息
     * @param specParam
     */
    public void addGroupParam(SpecParam specParam) {
        paramMapper.insert(specParam);
    }

    /**
     * 修改组下面的参数信息
     * @param specParam
     */
    public void editGroupParam(SpecParam specParam) {
        paramMapper.updateByPrimaryKey(specParam);
    }

    /**
     *  删除组下面得到参数
     * @param id
     */
    public void deleteGroupParamById(Long id) {
        paramMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据组id删除组
     * @param id
     */
    @Transactional
    public void deleteCategoryGroupsById(Long id) {
        Example example = new Example(SpecParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId", id);
        // 删除此分组下的所有参数
        paramMapper.deleteByExample(example);
        // 删除此分组
        groupMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据cid查询规格参数组和规格参数
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupWithParamByCid(Long cid) {
        // 查询规格参数组
        List<SpecGroup> groups = queryGroupsByCid(cid);
        groups.forEach(group -> {
            // 查询规格参数
            List<SpecParam> specParams = queryParams(group.getId(), null, null, null);
            group.setParams(specParams);
        });
        return groups;
    }
}
